package ywpt.web.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import ywpt.components.acegi.domain.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// 取得请求相关的ActionContext实例
		// 取得请求相关的ActionContext实例
		ActionContext ctx = invocation.getInvocationContext();
		HttpSession session = ServletActionContext.getRequest().getSession(false); 
		if ( session == null ) {
			System.out.println("LoginInterceptor has no  session");
			return "login";
		} 		
		System.out.println("LoginInterceptor has  session: id= "+session.getId());
		
		
		User user =  (User)session.getAttribute( "USER_SESSION" );
		// 如果没有登陆，或者登陆所有的用户名不是yuewei，都返回重新登陆

		if ( user != null ) {
			System.out.println("LoginInterceptor has  user: user.username= "+user.getUsername() );
			
			return invocation.invoke();
		} 
		
		
		System.out.println("LoginInterceptor has no  session");
		ctx.put("tip", "你还没有登录");
		return "login";
	}

}
